package com.android.equipment.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import com.android.equipment.R
import com.android.equipment.adapter.RelevanceDeviceAdapter
import com.android.equipment.bean.TeamGroupDeviceData
import com.android.equipment.bean.TeamGroupRelevanceData
import com.android.equipment.dao.Device
import com.android.equipment.dao.People
import com.android.equipment.databinding.DialogTeamGroupPeopleRelevanceDeviceBinding
import com.blankj.utilcode.util.ToastUtils
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.core.CenterPopupView

class TeamGroupPeopleRelevanceDeviceDialog(
    context: Context,
    private val people: People,
    private val deviceList: List<Device>,
    private val listener: OnConfirmListener

) : BottomPopupView(context) {
    val binding: DialogTeamGroupPeopleRelevanceDeviceBinding by lazy {
        DialogTeamGroupPeopleRelevanceDeviceBinding.bind(popupImplView)
    }

    val adapter: RelevanceDeviceAdapter by lazy {
        RelevanceDeviceAdapter(R.layout.item_relevance_device)
    }
    private val teamGroupDeviceDataList: MutableList<TeamGroupDeviceData> by lazy {
        mutableListOf()
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_team_group_people_relevance_device
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate() {
        super.onCreate()
        binding.tvPeople.text = "${people.peopleName}(${people.peoplePhone})"
        initRecycler()
        initEvent()
    }

    private fun initRecycler() {
        val flex = FlexboxLayoutManager(context).apply {
            flexDirection = FlexDirection.ROW
            flexWrap = FlexWrap.WRAP
            justifyContent = JustifyContent.FLEX_START
        }
        binding.rvRelevance.layoutManager = flex
        binding.rvRelevance.adapter = adapter
        adapter.setList(deviceList)

        adapter.setOnItemClickListener { adapter, view, position ->
            val device = adapter.getItem(position) as Device

            device.isSelect = !device.isSelect
            adapter.notifyItemChanged(position)

        }
    }

    private fun initEvent() {
        binding.tvConfirm.setOnClickListener {
            val count = adapter.data.count { it.isSelect }
            if (count > 0) {
                teamGroupDeviceDataList.clear()
                adapter.data.forEach {
                    if (it.isSelect) {
                        val teamGroupDevice = TeamGroupDeviceData(
                            deviceId = it.id,
                            deviceName = it.deviceName,
                            deviceCode = it.deviceCode
                        )
                        teamGroupDeviceDataList.add(teamGroupDevice)
                        it.isOccupy = true
                    }
                    it.isSelect = false
                }
                val teamGroupRelevanceData =TeamGroupRelevanceData(
                    peopleId = people.id,
                    peopleName = people.peopleName,
                    peoplePhone = people.peoplePhone,
                    peoplePhoto = people.peoplePhoto,
                    devices = teamGroupDeviceDataList
                )

                listener.onConfirm(adapter.data,teamGroupRelevanceData)
                dismiss()

            } else {
                ToastUtils.showShort("请选择要绑定的设备!")
            }
        }

        binding.tvCancel.setOnClickListener {
            dismiss()
        }
    }


    interface OnConfirmListener {
        fun onConfirm(deviceList: List<Device>,teamGroupRelevanceData: TeamGroupRelevanceData)
    }
}